package com.freemalll.merchant.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.freemalll.common.core.annotation.ResultInterceptor;
import com.freemalll.merchant.annotation.PreventDuplicateSubmit;
import com.freemalll.merchant.domain.dto.*;
import com.freemalll.merchant.domain.entity.MerMerchantRiskCapitalOrder;
import com.freemalll.merchant.domain.vo.*;
import com.freemalll.merchant.service.CollaborationApplyService;
import com.freemalll.merchant.service.MerMerchantRiskCapitalOrderService;
import com.freemalll.system.api.model.UserDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/collaboration-apply")
@Tag(name = "申请管理：合作申请")
@ResultInterceptor
@AllArgsConstructor
@Validated
public class CollaborationApplyController {

    private final CollaborationApplyService collaborationApplyService;
    private final MerMerchantRiskCapitalOrderService merMerchantRiskCapitalOrderService;

    @PostMapping("/page")
    @Operation(summary = "分页")
    public Page<CollaborationApplyVo> page(@RequestBody CollaborationApplyDto dto) {
        return collaborationApplyService.page(dto);
    }

    @PostMapping("statistics")
    @Operation(summary = "案件数量统计")
    public CollaborationApplyStatisticsVo statistics(@RequestBody CollaborationApplyDto dto) {
        return collaborationApplyService.statistics(dto);
    }

    @GetMapping("/getExistingMerchants")
    @Operation(summary = "获取存量商户列表")
    public List<Map<String, String>> getExistingMerchants(@RequestParam(value = "merId") String merId) {
        return collaborationApplyService.getExistingMerchants(merId);
    }

    @GetMapping("/getMerInfo")
    @Operation(summary = "获取商户详情")
    public MerchantInfoVo getMerInfoById(@RequestParam(value = "merId") String merId) {
        return collaborationApplyService.getMerInfoById(merId);
    }

    @PostMapping("risk/capital/all")
    @Operation(summary = "引擎返回的支持资方列表")
    public List<MerMerchantRiskCapitalOrder> listSearch(@RequestBody MerMerchantRiskCapitalOrderDto search) {
        return merMerchantRiskCapitalOrderService.listSearch(search);
    }

    @PostMapping("/submitPreliminaryReview")
    @Operation(summary = "提交初审")
    @PreventDuplicateSubmit()
    public SubmitPreliminaryReviewResultVo submitPreliminaryReview(@Valid @RequestBody SubmitPreliminaryReviewDto dto) {
        return collaborationApplyService.submitPreliminaryReview(dto);
    }

    @PostMapping("/submitLicenseInfo")
    @Operation(summary = "提交证照信息")
    @PreventDuplicateSubmit()
    public void submitLicenseInfo(@RequestBody SubmitLicenseInfoDto dto) {
        collaborationApplyService.submitLicenseInfo(dto);
    }

    @PostMapping("/submitBusinessInfo")
    @Operation(summary = "提交经营信息")
    @PreventDuplicateSubmit()
    public void submitBusinessInfo(@RequestBody SubmitBusinessInfoDto dto) {
        collaborationApplyService.submitBusinessInfo(dto);
    }

    @PostMapping("/submitOtherInfo")
    @Operation(summary = "提交其他资料")
    @PreventDuplicateSubmit()
    public SubmitOtherInfoDto submitOtherInfo(@RequestBody SubmitOtherInfoDto dto) {
        return collaborationApplyService.submitOtherInfo(dto);
    }

    @GetMapping("/withdraw/{merId}")
    @Operation(summary = "取消申请")
    @PreventDuplicateSubmit()
    public void withdraw(@PathVariable String merId) {
        collaborationApplyService.withdraw(merId);
    }

    @GetMapping("/getResidentPersonnel")
    @Operation(summary = "获取系统所有人员")
    public List<UserDto> getResidentPersonnel() {
        return collaborationApplyService.getResidentPersonnel();
    }

    @PostMapping(value = "/idCardOcr")
    @Operation(summary = "身份证ocr识别")
    public IdCardOrcVo idCardOcr(MultipartFile file) {
        return collaborationApplyService.idCardOcr(file);
    }

    @PostMapping(value = "/businessLicenseOcr")
    @Operation(summary = "营业执照ocr识别")
    public BusinessLicenseVo businessLicenseOcr(MultipartFile file) {
        return collaborationApplyService.businessLicenseOcr(file);
    }

    @GetMapping(value = "/checkMerShortName")
    @Operation(summary = "校验商户简称是否存在")
    public void checkMerShortName(@RequestParam(value = "merShortName") String merShortName, @RequestParam(value = "merId") String merId) {
        collaborationApplyService.checkMerShortName(merShortName, merId);
    }

    @PostMapping(value = "/accountLicenseOcr")
    @Operation(summary = "银行账户开户许可证ocr识别")
    public MerAccountLicenseVo accountLicenseOcr(MultipartFile file) {
        return collaborationApplyService.accountLicenseOcr(file);
    }

    @GetMapping(value = "/getMerAccountLicense")
    @Operation(summary = "根据统一信用代码营业执照信息")
    public MerBusinessLicenseVo getMerAccountLicense(@RequestParam(value = "licenseCode") String licenseCode) throws Exception {
        return collaborationApplyService.getMerAccountLicense(licenseCode);
    }

    @PostMapping(value = "/submitSupplementaryDocuments")
    @Operation(summary = "提交补件")
    public void submitSupplementaryDocuments(@RequestBody List<MerAttachDto> dto) {
        collaborationApplyService.submitSupplementaryDocuments(dto);
    }

    @GetMapping(value = "/getMerchantRateIRR")
    @Operation(summary = "根据商户id获取IRR年费率列表")
    public List<MerchantRateDto> getMerchantRateIRR(@RequestParam(value = "merId") String merId, @RequestParam(value = "sceneCategory") String sceneCategory) {
        return collaborationApplyService.getMerchantRateIRR(merId, sceneCategory);
    }

    @GetMapping(value = "/reapply")
    @Operation(summary = "重新申请")
    public String reapply(@RequestParam(value = "merId") String merId) {
        return collaborationApplyService.reapply(merId);
    }

}
